﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BL.Membership;
using BaseLib.Security;

namespace DA
{
    public static class RolesExtensions
    {
        private const int ROLES_ENCRYPTION_KEY = 1;
        public static UserPermission GetPermissions(this Role role)
        {
            long perms;
            AESEncryption encryption = new AESEncryption(ROLES_ENCRYPTION_KEY);
            string number = encryption.DecryptString(role.PermissionsString);

            UserPermission permissions = UserPermission.None;

            if (long.TryParse(number, out perms))
                permissions = (UserPermission)perms;

            return permissions;
        }

        public static void SetPermissions(this Role role, UserPermission perm)
        {
            AESEncryption encryption = new AESEncryption(ROLES_ENCRYPTION_KEY);
            role.PermissionsString = encryption.EncryptToString(((long)perm).ToString());
        }
    }

}
